﻿using System;
using System.Collections.Generic;
using System.Linq;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.Possy
{
    public partial class PartyPossyEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyPossyInfoAdd";
            }
        }

        public string Category
        {
            get
            {
                return "Article";
            }
        }
        #endregion

        #region 页面属性
        public string EditorContent
        {
            get; set;
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }

        private void LoadData()
        {
            
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyPossyInfos.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtName.Text = current.Name;
                this.txtAddress.Text = current.Address;
                this.txtDescribe.Text = current.Describe;
                this.txtLinkPhone.Text = current.LinkPhone;
                this.txtTraffic.Text = current.Traffic;
                this.ppBranch.Value = current.BranchID;
                this.ppBranch.Text = ControlBusiness.GetBranchPickerText(current.BranchID);
                this.ppPossyAdmin.Text = ControlBusiness.GetMembersPickerText(current.PossyAdmin);
                this.ppPossyAdmin.Value = current.PossyAdmin;
                this.cbxEnabled.Checked = current.IsShowPortal;
                LoadVisitDays(current.VisitDays);
            }


        }

        /// <summary>
        /// 加载接待时间
        /// </summary>
        /// <param name="VisitDays"></param>
        public void LoadVisitDays(string VisitDays)
        {
            if (string.IsNullOrEmpty(VisitDays.Trim()))
            {
                return;
            }
            var days = VisitDays.Split(',');
            foreach (var d in days)
            {
                foreach (CheckItem item in cblVisitDays.Items)
                {
                    if (item.Value == d)
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// 获取加载接待时间
        /// </summary>
        public string GetVisitDays()
        {
            List<string> days = new List<string>();
            foreach (CheckItem item in cblVisitDays.Items)
            {
                if (item.Selected)
                {
                    days.Add(item.Value.Trim());
                }
            }
            if (days.Count < 1)
                return "";
            return string.Join(",", days.ToArray());
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyPossyInfo possyInfo;
            if (id != "")
            {
                //更新
                possyInfo = DB.PartyPossyInfos.Find(id);
                if (possyInfo == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                possyInfo.ModifyTime = DateTime.Now;
                possyInfo.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                possyInfo = new PartyPossyInfo();
                possyInfo.ID = StringHelper.NewID;
                possyInfo.CreateTime = DateTime.Now;
                possyInfo.ModifyTime = DateTime.Now;
                possyInfo.CreateBy = CurrentUser.LoginID;
                possyInfo.ModifyBy = CurrentUser.LoginID;
                DB.PartyPossyInfos.Add(possyInfo);
            }

            possyInfo.Address = txtAddress.Text;
            possyInfo.BranchID = this.ppBranch.Value;
            possyInfo.Describe = this.txtDescribe.Text;
            possyInfo.IsShowPortal = this.cbxEnabled.Checked;
            possyInfo.LinkPhone = this.txtLinkPhone.Text;
            possyInfo.Name = this.txtName.Text;
            possyInfo.PossyAdmin = this.ppPossyAdmin.Value;
            possyInfo.Traffic = this.txtTraffic.Text;
            possyInfo.VisitDays = GetVisitDays();
           
            DB.SaveChanges();
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }


        #endregion

        protected void ppPossyAdmin_TextChanged(object sender, EventArgs e)
        {
            if (this.ppPossyAdmin.Value != "")
            {
                var admins = this.ppPossyAdmin.Value.Split(',');
                var q = DB.PartyOrgMembers.Where(a => admins.Contains(a.LoginID)).Select(a => a.MobileNo);
                if (q.Count() > 0)
                {
                    this.txtLinkPhone.Text = string.Join(",", q.ToArray());
                }

            }
        }

      
    }
}